package shuashuami.hb.com.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.listener.IDialogBtnClickListener;

/**
 * created by zhangqi at 2017/6/28
 */
public class DialogUtils extends Dialog{

    public static enum DialogStyle{
        EditDialog
    }

    DialogStyle mStyle;
    Context mContext;

    View mView;

    LinearLayout content;

    IDialogBtnClickListener listener;
    TextView text_left;
    TextView text_right;


    public DialogUtils(Context context,DialogStyle style) {
        super(context, R.style.Dialog);
        this.mStyle=style;
        this.mContext=context;
        setCustomDialog();
    }

    private void setCustomDialog() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_custom, null);

        initView(mView);
        super.setContentView(mView);
    }

    public void setMyTitle(String title){
        TextView textTitle= (TextView) mView.findViewById(R.id.dialog_title);
        textTitle.setText(title);
    }


    public void initView(View v){
        content= (LinearLayout) v.findViewById(R.id.dialog_content);
        if(mStyle==DialogStyle.EditDialog){

            EditText editText=new EditText(mContext);
            editText.setPadding(10,0,0,0);
            editText.setBackgroundResource(R.drawable.shape_corner_change_content_edt_bg);
            content.addView(editText);

        }

        text_left= (TextView) v.findViewById(R.id.btn_left);
        text_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onLeftBtnClick();
                    DialogUtils.this.dismiss();
                }
            }
        });

        text_right= (TextView) v.findViewById(R.id.btn_right);
        text_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    Bundle bundle=getValue();
                    listener.onRightBtnClick(bundle);
                    DialogUtils.this.dismiss();
                }
            }
        });
    }


    public Bundle getValue(){
        Bundle bundle=new Bundle();
        if(mStyle==DialogStyle.EditDialog){
            EditText text=(EditText) content.getChildAt(0);
            bundle.putString("0",text.getText().toString());
        }
        return bundle;
    }

    public void registerBtnClick(IDialogBtnClickListener listener){
        this.listener=listener;
    }

    public DialogUtils(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DialogUtils(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    // 自定义的对话框，标题，按钮，内容
}
